import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalRef, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';

@Component({
  selector: 'app-person-select',
  templateUrl: './person-select.component.html',
  styles: []
})
export class PersonSelectComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any[];
  loading = false;
  @Input() type:number
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
    this.config.defaultParam={"qcCode":value.qcCode,"isAdmin":0}
  };
  param: any;
  deviceType:string;
  submitDeviceType:string;
  show:boolean=false;
  config: SPage = {
    columns: [
      {
        index: 'name',
        title: '用户名称',
        filter: {
          placeholder: '请输入用户名',
          type: 'string'
        }
      },
      {
        index: 'phone',
        title: '联系电话',
        filter: {
          placeholder: '请输入联系电话',
          type: 'string'
        }
      },
      {
        index: 'qcCode',
        title: '二维码sn'
      },
      {
        index: 'description',
        title: '描述',
      },
      {
        index: 'createTime',
        title: "创建时间",
        type:ColumnType.date
      }
    ],
    showCheckBox: false,
    reqUrl:'/core/qrcode/peopleListByQcCode',
    editBtn: [
      {title: '选择'}
    ]
  }
  constructor(private http: _HttpClient,  private api: ApiService,private ref: NzModalRef, private modal: NzModalService, private message: NzMessageService) { }
  ngOnInit() {
    
  }
  operate(e: any): void {
    if (e.title == '选择') {
      if(this.type == 1){//转换
        this.submitData(e.data);
      }else if(this.type == 2){//解绑
        this.unbindCallBack({id:this.param.id,targetId:e.data.id})
      }
    }
  }
  _console(e: any): void {
    this.selectData = e;
  }
  dataProcess(data: STData[]){
    return data;
  }
  //提交数据
  submitData(data:any) {
    this.modal.confirm({
      nzTitle: '确定转换',
      nzOnOk: () => {
        this.http.post('/core/qrcode/editAdmin',{id:this.param.id,targetId:data.id}).subscribe(res => {
          if(res.status === '200'){
            this.message.success('转换成功');
            this.ref.triggerOk();
          }else{
            this.message.error(res.message)
          }
        })
      }
    })
  }
  //解绑
  unbindCallBack(data){
    this.modal.confirm({
      nzTitle: '是否确定?',
      nzOnOk: () => {
        this.http.request("post","/core/qrcode/unbindQrCode",{body:data}).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
            this.ref.triggerOk();
          }else{
            this.message.error(res.message)
          }
        })
      }
    })
  }
}
